function [phi,phi_dx,phi_dy] = eval_lag(d, x ,y)
% this funtion return the value of shape functions of degree d at points v
%  d ------ degree , must be positive integer
%  x and y is the corresponding value of L1 and L2 
% the derivtive must be comput with relate to the jacobi of element
L1 = x;
L2 = y;
L3 = 1-x-y;
switch d
case 1
    phi = [L1;L2;L3];
    
    phi_dx = [1;0;-1]*ones(1,length(x));
    
    phi_dy = [0;1;-1]*ones(1,length(x));
    
 case 2
    phi = [-L1 + 2*L1.^2; 4*L1.*L2; -L2 + 2*L2.^2; 4*L1.*L3;...    
     4*L2.*L3; -L3 + 2*L3.^2];

	 phi_dx = [4*L1-1; 4*L2; 0*ones(size(x)); 4*L3-4*L1; -4*L2 ; 1-4*L3 ];

	 phi_dy = [0*ones(size(x)); 4*L1;  4*L2-1; -4*L1; 4*L3-4*L2; 1-4*L3 ];
    
 case 3
    phi = 1/2*[L1.*(-2 + 3*L1).*(-1 + 3*L1); ...
          9*L1.*(-1 + 3*L1).*L2; ...
          9*L1.*L2.*(-1 + 3*L2); ...
          L2.*(-2 + 3*L2).*(-1 + 3*L2); ...
          9*L1.*(-1 + 3*L1).*L3; ...
          54*L1.*L3.*L2; ...
          9*L3.*L2.*(-1 + 3*L2); ...
          9*L1.*(-1 + 3*L3).*L3; ...
          9*(-1 + 3*L3).*L3.*L2; ...
          (-2 + 3*L3).*(-1 + 3*L3).*L3];    
    
    phi_dx = [ 1/2*(3*L1-1).*(3*L1-2)+3/2*L1.*(3*L1-2)+3/2*L1.*(3*L1-1); ...
                            9/2*(3*L1-1).*L2+27/2*L1.*L2; ...
                                       9/2*L2.*(3*L2-1); ...
                                       0*ones(size(x)); ...
            9/2*(3*L1-1).*L3+27/2*L1.*L3-9/2*L1.*(3*L1-1); ...
                                     27*L2.*L3-27*L1.*L2; ...
                                      -9/2*L2.*(3*L2-1); ...
            9/2*L3.*(3*L3-1)-9/2*L1.*(3*L3-1)-27/2*L1.*L3; ...
                           -9/2*L2.*(3*L3-1)-27/2*L2.*L3; ...
-1/2*(3*L3-1).*(3*L3-2)-3/2*L3.*(3*L3-2)-3/2*L3.*(3*L3-1)  ];
    
    phi_dy = [                          0*ones(size(x)); ...
                                        9/2*L1.*(3*L1-1); ...
                             9/2*L1.*(3*L2-1)+27/2*L1.*L2; ...
  1/2*(3*L2-1).*(3*L2-2)+3/2*L2.*(3*L2-2)+3/2*L2.*(3*L2-1); ...
                                       -9/2*L1.*(3*L1-1); ...
                                      27*L1.*L3-27*L1.*L2; ...
             9/2*(3*L2-1).*L3+27/2*L2.*L3-9/2*L2.*(3*L2-1); ...
                            -9/2*L1.*(3*L3-1)-27/2*L1.*L3; ...
             9/2*L3.*(3*L3-1)-9/2*L2.*(3*L3-1)-27/2*L2.*L3; ...
 -1/2*(3*L3-1).*(3*L3-2)-3/2*L3.*(3*L3-2)-3/2*L3.*(3*L3-1) ]; 
    
 case 4
    phi = 1/6 *[L1.*(-3 + 4*L1).*(-2 + 4*L1).*(-1 + 4*L1);...
          16*L1.*(-2 + 4*L1).*(-1 + 4*L1).*L2;...
          24*L1.*(-1 + 4*L1).*L2.*(-1 + 4*L2);...
          16*L1.*L2.*(-2 + 4*L2).*(-1 + 4*L2);...
          L2.*(-3 + 4*L2).*(-2 + 4*L2).*(-1 + 4*L2);...
          16*L1.*(-2 + 4*L1).*(-1 + 4*L1).*L3;...
          192*L1.*(-1 + 4*L1).*L2.*L3; 192*L1.*L2.*(-1 + 4*L2).*L3;...
          16*L2.*(-2 + 4*L2).*(-1 + 4*L2).*L3;...
          24*L1.*(-1 + 4*L1).*L3.*(-1 + 4*L3);... 
          192*L1.*L2.*L3.*(-1 + 4*L3); 24*L2.*(-1 + 4*L2).*L3.*(-1 + 4*L3);...
          16*L1.*L3.*(-2 + 4*L3).*(-1 + 4*L3); ...
          16*L2.*L3.*(-2 + 4*L3).*(-1 + 4*L3);...
          L3.*(-3 + 4*L3).*(-2 + 4*L3).*(-1 + 4*L3)];
    
    phi_dx = [         1/3*(8*L1-3).*(16*L1.^2-12*L1+1);...
                               16/3*L2.*(1-12*L1+24*L1.^2);...
                                 4*L2.*(-1+8*L1).*(4*L2-1);...
                               16/3*L2.*(4*L2-1).*(2*L2-1);...
                                      zeros(size(x));...
 16/3*L3-64*L1.*L3+128*L3.*L1.^2-16/3*L1+32.*L1.^2-128/3*L1.^3;...
                           32*L2.*(-L3+8*L1.*L3+L1-4*L1.^2);...
                                  32*L2.*(4*L2-1).*(L3-L1);...
                              -16/3*L2.*(4*L2-1).*(2*L2-1);...
                       4*(-4*L3+32*L1.*L3+1-4*L1).*(L3-L1);...
                            32*L2.*(4*L3.^2-L3-8*L1.*L3+L1);...
                                 -4*L2.*(4*L2-1).*(8*L3-1);...
 128/3*L3.^3-32*L3.^2+16/3*L3-128*L1.*L3.^2+64*L1.*L3-16/3*L1;...
                              -16/3*L2.*(24*L3.^2-12*L3+1);...
                         -1/3*(8*L3-3).*(16*L3.^2-12*L3+1)];
    
  phi_dy(1:4,:) = [zeros(size(x)); 16/3*L1.*(-1+4*L1).*(-1+2*L1); 4*L1.*(-1+4*L1).*(8*L2-1); 16/3*L1.*(24*L2.^2-12*L2+1)]; 
  phi_dy(5:7,:) = [1/3*(8*L2-3).*(16*L2.^2-12*L2+1); -16/3*L1.*(-1+4*L1).*(-1+2*L1); 32*L1.*(-1+4*L1).*(-L2+L3)];
  phi_dy(8:9,:) = [32*L1.*(8*L2.*L3-L3-4*L2.^2+L2); 128*L2.^2.*L3-64*L2.*L3+16/3*L3-128/3*L2.^3+32*L2.^2-16/3*L2];
  phi_dy(10:12,:) = [-4*L1.*(-1+4*L1).*(8*L3-1); 32*L1.*(4*L3.^2-L3-8*L2.*L3+L2); 4*(32*L2.*L3-4*L3+1-4*L2).*(-L2+L3)];
  phi_dy(13:14,:) = [-16/3*L1.*(24*L3.^2-12*L3+1); 128/3*L3.^3-32*L3.^2+16/3*L3-128*L2.*L3.^2+64*L2.*L3-16/3*L2];
  phi_dy(15,:) = [-1/3*(8*L3-3).*(16*L3.^2-12*L3+1)];
  
case 5
   phi(1,:) = 1/24*L1.*(5*L1-1).*(5*L1-2).*(5*L1-3).*(5*L1-4); 
phi(2,:) =      25/24*L1.*(5*L1-1).*(5*L1-2).*(5*L1-3).*L2; 
phi(3,:) =      25/12*L1.*(5*L1-1).*(5*L1-2).*L2.*(5*L2-1); 
phi(4,:) =      25/12*L1.*(5*L1-1).*L2.*(5*L2-1).*(5*L2-2); 
phi(5,:) =      25/24*L1.*L2.*(5*L2-1).*(5*L2-2).*(5*L2-3); 
phi(6,:) = 1/24*L2.*(5*L2-1).*(5*L2-2).*(5*L2-3).*(5*L2-4); 
phi(7,:) =      25/24*L1.*(5*L1-1).*(5*L1-2).*(5*L1-3).*L3; 
phi(8,:) =            125/6*L1.*(5*L1-1).*(5*L1-2).*L2.*L3; 
phi(9,:) =            125/4*L1.*(5*L1-1).*L2.*(5*L2-1).*L3; 
phi(10,:) =            125/6*L1.*L2.*(5*L2-1).*(5*L2-2).*L3;
phi(11,:) =      25/24*L2.*(5*L2-1).*(5*L2-2).*(5*L2-3).*L3;
phi(12,:) =      25/12*L1.*(5*L1-1).*(5*L1-2).*L3.*(5*L3-1);   
phi(13,:) =            125/4*L1.*(5*L1-1).*L2.*L3.*(5*L3-1);
phi(14,:) =            125/4*L1.*L2.*(5*L2-1).*L3.*(5*L3-1);
phi(15,:) =      25/12*L2.*(5*L2-1).*(5*L2-2).*L3.*(5*L3-1);
phi(16,:) =      25/12*L1.*(5*L1-1).*L3.*(5*L3-1).*(5*L3-2);
phi(17,:) =            125/6*L1.*L2.*L3.*(5*L3-1).*(5*L3-2);
phi(18,:) =      25/12*L2.*(5*L2-1).*L3.*(5*L3-1).*(5*L3-2);
phi(19,:) =      25/24*L1.*L3.*(5*L3-1).*(5*L3-2).*(5*L3-3);
phi(20,:) =      25/24*L2.*L3.*(5*L3-1).*(5*L3-2).*(5*L3-3);
phi(21,:) = 1/24*L3.*(5*L3-1).*(5*L3-2).*(5*L3-3).*(5*L3-4);

phi_dx = [  1/24.*(5.*L1-1).*(5.*L1-2).*(5.*L1-3).*(5.*L1-4)+5/24.*L1.*(5.*L1-2).*(5.*L1-3).*(5.*L1-4)+5/24.*L1.*(5.*L1-1).*(5.*L1-3).*(5.*L1-4)+5/24.*L1.*(5.*L1-1).*(5.*L1-2).*(5.*L1-4)+5/24.*L1.*(5.*L1-1).*(5.*L1-2).*(5.*L1-3);... 
                                                      25/24.*(5.*L1-1).*(5.*L1-2).*(5.*L1-3).*L2+125/24.*L1.*(5.*L1-2).*(5.*L1-3).*L2+125/24.*L1.*(5.*L1-1).*(5.*L1-3).*L2+125/24.*L1.*(5.*L1-1).*(5.*L1-2).*L2;...            
                                                                                     25/12.*(5.*L1-1).*(5.*L1-2).*L2.*(5.*L2-1)+125/12.*L1.*(5.*L1-2).*L2.*(5.*L2-1)+125/12.*L1.*(5.*L1-1).*L2.*(5.*L2-1);...                  
                                                                                                                    25/12.*(5.*L1-1).*L2.*(5.*L2-1).*(5.*L2-2)+125/12.*L1.*L2.*(5.*L2-1).*(5.*L2-2);...                        
                                                                                                                                                   25/24.*L2.*(5.*L2-1).*(5.*L2-2).*(5.*L2-3);...                              
                                                                                                                                                                                     zeros(size(x));...                        
                  25/24.*(5.*L1-1).*(5.*L1-2).*(5.*L1-3).*L3+125/24.*L1.*(5.*L1-2).*(5.*L1-3).*L3+125/24.*L1.*(5.*L1-1).*(5.*L1-3).*L3+125/24.*L1.*(5.*L1-1).*(5.*L1-2).*L3-25/24.*L1.*(5.*L1-1).*(5.*L1-2).*(5.*L1-3);...     
                                                                           125/6.*(5.*L1-1).*(5.*L1-2).*L2.*L3+625/6.*L1.*(5.*L1-2).*L2.*L3+625/6.*L1.*(5.*L1-1).*L2.*L3-125/6.*L1.*(5.*L1-1).*(5.*L1-2).*L2;...               
                                                                                                   125/4.*(5.*L1-1).*L2.*(5.*L2-1).*L3+625/4.*L1.*L2.*(5.*L2-1).*L3-125/4.*L1.*(5.*L1-1).*L2.*(5.*L2-1);...                    
                                                                                                                           125/6.*L2.*(5.*L2-1).*(5.*L2-2).*L3-125/6.*L1.*L2.*(5.*L2-1).*(5.*L2-2);...                         
                                                                                                                                                  -25/24.*L2.*(5.*L2-1).*(5.*L2-2).*(5.*L2-3);...                              
                  25/12.*(5.*L1-1).*(5.*L1-2).*L3.*(5.*L3-1)+125/12.*L1.*(5.*L1-2).*L3.*(5.*L3-1)+125/12.*L1.*(5.*L1-1).*L3.*(5.*L3-1)-25/12.*L1.*(5.*L1-1).*(5.*L1-2).*(5.*L3-1)-125/12.*L1.*(5.*L1-1).*(5.*L1-2).*L3;...     
                                                                           125/4.*(5.*L1-1).*L2.*L3.*(5.*L3-1)+625/4.*L1.*L2.*L3.*(5.*L3-1)-125/4.*L1.*(5.*L1-1).*L2.*(5.*L3-1)-625/4.*L1.*(5.*L1-1).*L2.*L3;...               
                                                                                                   125/4.*L2.*(5.*L2-1).*L3.*(5.*L3-1)-125/4.*L1.*L2.*(5.*L2-1).*(5.*L3-1)-625/4.*L1.*L2.*(5.*L2-1).*L3;...                    
                                                                                                                   -25/12.*L2.*(5.*L2-1).*(5.*L2-2).*(5.*L3-1)-125/12.*L2.*(5.*L2-1).*(5.*L2-2).*L3;...                        
                  25/12.*(5.*L1-1).*L3.*(5.*L3-1).*(5.*L3-2)+125/12.*L1.*L3.*(5.*L3-1).*(5.*L3-2)-25/12.*L1.*(5.*L1-1).*(5.*L3-1).*(5.*L3-2)-125/12.*L1.*(5.*L1-1).*L3.*(5.*L3-2)-125/12.*L1.*(5.*L1-1).*L3.*(5.*L3-1);...     
                                                                           125/6.*L2.*L3.*(5.*L3-1).*(5.*L3-2)-125/6.*L1.*L2.*(5.*L3-1).*(5.*L3-2)-625/6.*L1.*L2.*L3.*(5.*L3-2)-625/6.*L1.*L2.*L3.*(5.*L3-1);...               
                                                                                    -25/12.*L2.*(5.*L2-1).*(5.*L3-1).*(5.*L3-2)-125/12.*L2.*(5.*L2-1).*L3.*(5.*L3-2)-125/12.*L2.*(5.*L2-1).*L3.*(5.*L3-1);...                  
                  25/24.*L3.*(5.*L3-1).*(5.*L3-2).*(5.*L3-3)-25/24.*L1.*(5.*L3-1).*(5.*L3-2).*(5.*L3-3)-125/24.*L1.*L3.*(5.*L3-2).*(5.*L3-3)-125/24.*L1.*L3.*(5.*L3-1).*(5.*L3-3)-125/24.*L1.*L3.*(5.*L3-1).*(5.*L3-2);...     
                                                     -25/24.*L2.*(5.*L3-1).*(5.*L3-2).*(5.*L3-3)-125/24.*L2.*L3.*(5.*L3-2).*(5.*L3-3)-125/24.*L2.*L3.*(5.*L3-1).*(5.*L3-3)-125/24.*L2.*L3.*(5.*L3-1).*(5.*L3-2);...            
 -1/24.*(5.*L3-1).*(5.*L3-2).*(5.*L3-3).*(5.*L3-4)-5/24.*L3.*(5.*L3-2).*(5.*L3-3).*(5.*L3-4)-5/24.*L3.*(5.*L3-1).*(5.*L3-3).*(5.*L3-4)-5/24.*L3.*(5.*L3-1).*(5.*L3-2).*(5.*L3-4)-5/24.*L3.*(5.*L3-1).*(5.*L3-2).*(5.*L3-3) ];

phi_dy = [                                                                                                                                               zeros(size(x));...                                                                          
                                                                                                                                       25/24.*L1.*(5.*L1-1).*(5.*L1-2).*(5.*L1-3);...                                                      
                                                                                                  25/12.*L1.*(5.*L1-1).*(5.*L1-2).*(5.*L2-1)+125/12.*L1.*(5.*L1-1).*(5.*L1-2).*L2;...                                                      
                                                             25/12.*L1.*(5.*L1-1).*(5.*L2-1).*(5.*L2-2)+125/12.*L1.*(5.*L1-1).*L2.*(5.*L2-2)+125/12.*L1.*(5.*L1-1).*L2.*(5.*L2-1);...                                                      
                        25/24.*L1.*(5.*L2-1).*(5.*L2-2).*(5.*L2-3)+125/24.*L1.*L2.*(5.*L2-2).*(5.*L2-3)+125/24.*L1.*L2.*(5.*L2-1).*(5.*L2-3)+125/24.*L1.*L2.*(5.*L2-1).*(5.*L2-2);...                                                      
               1/24.*(5.*L2-1).*(5.*L2-2).*(5.*L2-3).*(5.*L2-4)+5/24.*L2.*(5.*L2-2).*(5.*L2-3).*(5.*L2-4)+5/24.*L2.*(5.*L2-1).*(5.*L2-3).*(5.*L2-4)+5/24.*L2.*(5.*L2-1).*(5.*L2-2).*(5.*L2-4)+5/24.*L2.*(5.*L2-1).*(5.*L2-2).*(5.*L2-3);...
                                                                                                                                             -25/24.*L1.*(5.*L1-1).*(5.*L1-2).*(5.*L1-3);...                                               
                                                                                                            125/6.*L1.*(5.*L1-1).*(5.*L1-2).*L3-125/6.*L1.*(5.*L1-1).*(5.*L1-2).*L2;...                                                    
                                                                               125/4.*L1.*(5.*L1-1).*(5.*L2-1).*L3+625/4.*L1.*(5.*L1-1).*L2.*L3-125/4.*L1.*(5.*L1-1).*L2.*(5.*L2-1);...                                                    
                                                  125/6.*L1.*(5.*L2-1).*(5.*L2-2).*L3+625/6.*L1.*L2.*(5.*L2-2).*L3+625/6.*L1.*L2.*(5.*L2-1).*L3-125/6.*L1.*L2.*(5.*L2-1).*(5.*L2-2);...                                                    
        25/24.*(5.*L2-1).*(5.*L2-2).*(5.*L2-3).*L3+125/24.*L2.*(5.*L2-2).*(5.*L2-3).*L3+125/24.*L2.*(5.*L2-1).*(5.*L2-3).*L3+125/24.*L2.*(5.*L2-1).*(5.*L2-2).*L3-25/24.*L2.*(5.*L2-1).*(5.*L2-2).*(5.*L2-3);...                           
                                                                                                              -25/12.*L1.*(5.*L1-1).*(5.*L1-2).*(5.*L3-1)-125/12.*L1.*(5.*L1-1).*(5.*L1-2).*L3;...                                         
                                                                                    125/4.*L1.*(5.*L1-1).*L3.*(5.*L3-1)-125/4.*L1.*(5.*L1-1).*L2.*(5.*L3-1)-625/4.*L1.*(5.*L1-1).*L2.*L3;...                                               
                                                       125/4.*L1.*(5.*L2-1).*L3.*(5.*L3-1)+625/4.*L1.*L2.*L3.*(5.*L3-1)-125/4.*L1.*L2.*(5.*L2-1).*(5.*L3-1)-625/4.*L1.*L2.*(5.*L2-1).*L3;...                                               
        25/12.*(5.*L2-1).*(5.*L2-2).*L3.*(5.*L3-1)+125/12.*L2.*(5.*L2-2).*L3.*(5.*L3-1)+125/12.*L2.*(5.*L2-1).*L3.*(5.*L3-1)-25/12.*L2.*(5.*L2-1).*(5.*L2-2).*(5.*L3-1)-125/12.*L2.*(5.*L2-1).*(5.*L2-2).*L3;...                           
                                                                               -25/12.*L1.*(5.*L1-1).*(5.*L3-1).*(5.*L3-2)-125/12.*L1.*(5.*L1-1).*L3.*(5.*L3-2)-125/12.*L1.*(5.*L1-1).*L3.*(5.*L3-1);...                                   
                                                            125/6.*L1.*L3.*(5.*L3-1).*(5.*L3-2)-125/6.*L1.*L2.*(5.*L3-1).*(5.*L3-2)-625/6.*L1.*L2.*L3.*(5.*L3-2)-625/6.*L1.*L2.*L3.*(5.*L3-1);...                                          
   25/12.*(5.*L2-1).*L3.*(5.*L3-1).*(5.*L3-2)+125/12.*L2.*L3.*(5.*L3-1).*(5.*L3-2)-25/12.*L2.*(5.*L2-1).*(5.*L3-1).*(5.*L3-2)-125/12.*L2.*(5.*L2-1).*L3.*(5.*L3-2)-125/12.*L2.*(5.*L2-1).*L3.*(5.*L3-1);...                                
                                                -25/24.*L1.*(5.*L3-1).*(5.*L3-2).*(5.*L3-3)-125/24.*L1.*L3.*(5.*L3-2).*(5.*L3-3)-125/24.*L1.*L3.*(5.*L3-1).*(5.*L3-3)-125/24.*L1.*L3.*(5.*L3-1).*(5.*L3-2);...                             
      25/24.*L3.*(5.*L3-1).*(5.*L3-2).*(5.*L3-3)-25/24.*L2.*(5.*L3-1).*(5.*L3-2).*(5.*L3-3)-125/24.*L2.*L3.*(5.*L3-2).*(5.*L3-3)-125/24.*L2.*L3.*(5.*L3-1).*(5.*L3-3)-125/24.*L2.*L3.*(5.*L3-1).*(5.*L3-2);...                             
 -1/24.*(5.*L3-1).*(5.*L3-2).*(5.*L3-3).*(5.*L3-4)-5/24.*L3.*(5.*L3-2).*(5.*L3-3).*(5.*L3-4)-5/24.*L3.*(5.*L3-1).*(5.*L3-3).*(5.*L3-4)-5/24.*L3.*(5.*L3-1).*(5.*L3-2).*(5.*L3-4)-5/24.*L3.*(5.*L3-1).*(5.*L3-2).*(5.*L3-3) ];

otherwise
   fprintf('Sorry! Have not implement such high order finite element yet!');
end